
package com.ce.cases.source.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

import com.ce.cases.source.dao.po.CaseSource;
import com.ce.cases.source.dao.po.CaseSourceApprove;
import com.ce.cases.source.dao.po.CaseSourceDrawApprove;
import com.ce.cases.source.dao.po.CaseSourceFiles;
import com.ce.framework.constants.WebConstants;
import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.datamaintain.dao.po.AbstractPO;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.LogUtil;
import com.ce.framework.util.StringUtil;
import com.ce.framework.util.node.JsonTreeNode;
import com.ce.sys.login.model.User;
import com.ce.sys.org.dao.po.SysVOrg;
import com.ce.sys.person.dao.po.SysPerson;
import com.ce.util.FlowPicView;
import com.ce.util.FlowWorkUtil;
import com.ce.util.OrgUtil;
import com.ce.workflow.work.dao.po.WorkflowFlow;
import com.ce.workflow.work.dao.po.WorkflowWork;


/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2014-03-18
* 创建时间：15:29:27
* 功能描述：备选案源台账 [CASE_SOURCE]更改时用的Action
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class CaseSourceSendAction extends DataMaintainAction 
{
	private static final long serialVersionUID = 1L;
	
	private CaseSourceApprove appr;
	private CaseSourceDrawApprove drawAppr;
	
	@Override
	protected void initOtherData(boolean isNew) {
		if(isNew){
			try {
				BeanAccessUtil.copyProperty(po, "registerPerson", User.getUserName());
				BeanAccessUtil.copyProperty(po, "registerDate", new Date());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			
		}
	}
	@Override
	protected String addItem() throws AppException {
		try {
			BeanAccessUtil.copyProperty(po, "registerDeptId", User.getUserOrgId());
			BeanAccessUtil.copyProperty(po, "registerDeptName", User.getUserOrgName());
			BeanAccessUtil.copyProperty(po, "state", "0");
			BeanAccessUtil.copyProperty(po, "stateInfo", "备选案源");
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return super.addItem();
	}
	@Override
	protected String modifyItem() throws AppException {
		//避免将流程状态暴漏给页面，避免非法篡改
		AbstractPO entiy=(AbstractPO)this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
		BeanAccessUtil.copyNoCopyNullProperty(po, entiy);
		this.baselogic.updatePo(entiy,this.getAddList(),this.getModifyList(),this.getDeleteList());
	    return super.searchData();			
	}
	@Override
	public String look() throws AppException {
		super.look();
		return WebConstants.REQUEST_LOOKDATA_OPERATOR;
	}
	public String lookItemFrame(){
		return "lookItemFrame";
	}
	public void wsTree(){
		String vcRecordId=this.getParameter("vcRecordId");
		String hql="from CaseSourceFiles t where t.vcRecordId='"+vcRecordId+"' order by t.dtDate";
		List<CaseSourceFiles>  files=new ArrayList<CaseSourceFiles>();
		try {
			files=(List<CaseSourceFiles>) this.baselogic.searchByHql(hql);
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONArray jsonTree = new JSONArray();
		List<JsonTreeNode> fileList = new ArrayList<JsonTreeNode>();
		JsonTreeNode rootNode=new JsonTreeNode();
		rootNode.setId("1");
		rootNode.setIconCls("ext-icon-book_open");
		rootNode.setText("案源文书");
		rootNode.setState("open");
		if(files!=null&&files.size()>0){
			for(CaseSourceFiles file:files){
				JsonTreeNode child = new JsonTreeNode();
            	child.setId(file.getVcFileId());
            	child.setText(file.getVcFileName());
            	child.setIconCls("ext-icon-book_open");
            	fileList.add(child);
			}
			rootNode.setChildren(fileList);
		}
		List<JsonTreeNode> nodeList = new ArrayList<JsonTreeNode>();
		nodeList.add(rootNode);
		jsonTree.add(nodeList); // 写入树节点信息
		// 以字符串为文件流进行输出
		// 由于形成的json格式与easyui的格式在外部多了个[]，因此在这里进行移除。
		writeHTML(jsonTree.toString().substring(1, jsonTree.toString().length() - 1).toString()); // 将树以JSON格式的字符串进行输出

		
	}
	public String caseSourceHandOver(){
		String vcParId=User.getParOrgIdByOrgLevel("2");
		List<SysVOrg> deps=OrgUtil.getChildDep(vcParId);
		
		StringBuffer op = new StringBuffer();
		if(deps!=null&&deps.size()>0){
			for(SysVOrg org:deps){
				    op.append("<option value='"+org.getVcId()+"'>"+org.getVcName()+"</option>");
			}
			
		}else{
			op.append("<option value=''></option>");
		}
		this.setAttribute("deps", deps);
		return "caseSourceHandOver";
	}
	
	public String handOverSuccessed(){
		String chkSelf=this.getParameter("chkSelf");
		String[] userids = this.getParameterValues("userIds[]");
		String[] username=this.getParameterValues("idnames[]");
		String vcOrgId=this.getParameter("vcOrgId");
		String vcType=this.getParameter("vcType");
		String vcAdvance=this.getParameter("vcAdvance");
		SysVOrg org=OrgUtil.getOrg(vcOrgId);
		CaseSource po=null;
		try {
			po = (CaseSource) this.baselogic.getPOByPk(CaseSource.class.getName(), chkSelf);
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		FlowWorkUtil.transferFlowWork(chkSelf, vcType, User.getUserId(), "", "", vcOrgId, org.getVcName(), userids[0], username[0], vcAdvance);		
		return this.returnInfoForJS(true, null);
	}

	public String ajaxPersons(){
		String vcOrgId=this.getParameter("vcOrgId");
		List<SysPerson> persons=OrgUtil.getUserByOrgId(vcOrgId, true);
		
		this.rows=persons;
		return JSON;
	}
	/**
	 * 案源退回界面
	 * @return
	 */
	public String caseSourceGoBack(){
		return "caseSourceGoBack";
	}
	
	/**
	 * 案源退回操作
	 * @return
	 */
	public String caseSourceGoBackSuccess(){
		String vcId=this.getParameter("chkSelf");
		int state=FlowWorkUtil.backFlowWork(vcId,  User.getUserOrgId(), User.getUserOrgName(), User.getUserId(), User.getUserName(), "");	
		String info="";
		if(0==state){
			WorkflowWork curWork=FlowWorkUtil.getWorkflowWork(vcId);
			WorkflowFlow curFlow=FlowWorkUtil.getWorkflowFlowByVcSeq(vcId,curWork.getVcStatus());
			info="对不起，您无法退回，当前案源应有"+curFlow.getVcHandleOrgName()+curFlow.getVcHandlePersonName()+"进行退回。";
		}else if(1==state){
			info="签收成功！";
		}else if(2==state){
			info="对不起，不能重复退回！";
		}
		return this.returnInfoForJS(true, info);
	}
	
	public String caseSourceFlowPage(){
		List<FlowPicView> list = new ArrayList<FlowPicView>();
		
		list.add(new FlowPicView("选案","于2014-04-01"+
				"张三移交给李四","comp"));
		list.add(new FlowPicView("检查","于2014-04-01"+
				"张三移交给李四","comp"));
		list.add(new FlowPicView("审理","","ing"));
		list.add(new FlowPicView("执行","","unf"));
		JSONArray ary = new JSONArray();
		ary.addAll(list);
		setAttribute("flowpic",ary.toString());
		return "caseSourceFlowPage";
	}
	
	public CaseSourceApprove getAppr() {
		return appr;
	}
	public void setAppr(CaseSourceApprove appr) {
		this.appr = appr;
	}
	public CaseSourceDrawApprove getDrawAppr() {
		return drawAppr;
	}
	public void setDrawAppr(CaseSourceDrawApprove drawAppr) {
		this.drawAppr = drawAppr;
	}

}

